import React, { Component } from 'react';
import { connect } from 'dva';
import { Modal, Form, Input, InputNumber } from 'antd';
const FormItem = Form.Item;

class DataCenterModal extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
    };
  }

  showModelHandler = (e) => {
    if (e) e.stopPropagation();
    this.props.form.resetFields();
    this.setState({
      visible: true,
    });
  };

  hideModelHandler = () => {
    this.setState({
      visible: false,
    });
  };

  okHandler = () => {
    const { onOk } = this.props;
    this.props.form.validateFields((err, values) => {
      if (!err) {
        onOk(values);
        this.hideModelHandler();
      }
    });
  };

  render() {
    const { children,  title } = this.props;
    const { getFieldDecorator } = this.props.form;
    const { name,  city } = this.props.record;
    const formItemLayout = {
      labelCol: { span: 6 },
      wrapperCol: { span: 14 },
    };

    return (
        <span>
            <span onClick={this.showModelHandler}>
                { children }
            </span>
            <Modal
                confirmLoading={false}
                title={title}
                visible={this.state.visible}
                onOk={this.okHandler}
                onCancel={this.hideModelHandler}
            >
              <Form layout='horizontal' onSubmit={this.okHandler}>
                <FormItem
                  {...formItemLayout}
                  hasFeedback
                  label="名称"
                >
                  {
                    getFieldDecorator('name', {
                      initialValue: name,
                      rules: [
                        { required: true, whitespace: true, message: '请输入名称'},
                        { max: 50, whitespace: true, message: '名称不能超过50个字'},
                        ]
                    })(<Input />)
                  }
                </FormItem>
               
                <FormItem
                  {...formItemLayout}
                  hasFeedback
                  label="所在城市"
                >
                  {
                    getFieldDecorator('city', {
                      initialValue: city,
                      rules: [
                        {required: true, whitespace: true, message: '请输入所在城市' },
                        { max: 50, whitespace: true, message: '所在城市不能超过50个字'},]
                    })(<Input />)
                  }
                </FormItem>
               
              </Form>
            </Modal> 
        </span>
    );
  }
}

export default Form.create()(DataCenterModal);